﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class KetQuaHocRepository : IKetQuaHocRepository
    {
        DangKyPhuEntities entities;

        public KetQuaHocRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region repos
        IChuongTrinhDaoTaoRepository ctdtRepos = new ChuongTrinhDaoTaoRepository();
        IMonHocRepository monHocRepos = new MonHocRepository();
        #endregion

        /// <summary>
        /// lay cac kqht cua sv theo mon hoc cua tat ca hoc ky
        /// </summary>
        /// <param name="monHocID"></param>
        /// <param name="sinhVienID"></param>
        /// <param name="hockyID"></param>
        /// <returns></returns>
        public List<Entities.KetQuaHoc> LayKetQuaHocTapTheoSVMonHoc(string monHocID, string sinhVienID, IQueryable<int> hockyIDList)
        {
            List<KetQuaHoc> result = new List<KetQuaHoc>();
            foreach (var hocKyItem in hockyIDList)
            {
                var kqht = from m in entities.KetQuaHocs
                           where m.MSSV.Equals(sinhVienID)
                           && m.MonHocID.Equals(monHocID)
                           && m.HocKyID == hocKyItem
                           select m;
                foreach (var kqhtItem in kqht)
                    result.Add(kqhtItem);
            }
            return result;
        }

        /// <summary>
        /// tra ve kqht co dtb mh hoc cao nhat trong list duoc xet
        /// </summary>
        /// <param name="kqhtList"></param>
        /// <returns></returns>
        public KetQuaHoc LayKetQuaCaoNhat(List<KetQuaHoc> kqhtList)
        {
            KetQuaHoc max = kqhtList[0];
            for (int i = 1; i < kqhtList.Count; i++)
            {
                if (max.DTB == null)
                {
                    max = kqhtList[i];
                    continue;
                }
                if (kqhtList[i].DTB != null)
                {
                    if (kqhtList[i].DTB.Value > max.DTB)
                        max = kqhtList[i];
                }
            }
            return max;
        }

        public IQueryable<KetQuaHoc> LayKetQuaHocTapTheoSVMonHoc(string monHocID, string sinhVienID)
        {
            return (from m in entities.KetQuaHocs
                    where m.MSSV.Equals(sinhVienID)
                    && m.MonHocID.Equals(monHocID)
                    select m);
        }

        /// <summary>
        /// Lay ket qua hoc tap cao nhat cua sv theo ma sv va ma mon hoc
        /// </summary>
        /// <param name="monHocID"></param>
        /// <param name="sinhVienID"></param>
        /// <returns></returns>
        public KetQuaHoc LayKetQuaHocTapCaoNhatTheoSVMonHoc(string monHocID, string sinhVienID)
        {
            var result = (from m in entities.KetQuaHocs
                          where m.MSSV.Equals(sinhVienID)
                          && m.MonHocID.Equals(monHocID)
                          select m);
            if (result.Count() > 0)
                return LayKetQuaCaoNhat(result.ToList());
            return null;
        }

        public float TinhDTBTichLuy(SinhVien sv, string NganhID, string HeDaoTaoID)
        {
            //ISinhVienRepository svRepos = new SinhVienRepository();
            //SinhVien sv = svRepos.LaySVDangNhap(MSSV);
            var monHocIDTrongCTDT = ctdtRepos.LayMonHocIDChuongTrinhDaoTao(NganhID, sv.Lop.NienKhoaID.Value, HeDaoTaoID).ToList();
            float DTB = 0;
            float SoTC = 0;

            //cac mon hoc trong ctdt cua nganh, hedaotao cua sv
            //
            //foreach (var item in monHocIDTrongCTDT)
            //{
            var monHocCTDT = from m in entities.KetQuaHocs
                             where m.MSSV.Equals(sv.MSSV)
                             && monHocIDTrongCTDT.Contains(m.MonHocID)
                             select m;
            //}
            List<string> maxResultList = new List<string>();
            foreach (var monHocItem in monHocCTDT)
            {
                var result = LayKetQuaHocTapTheoSVMonHoc(monHocItem.MonHocID, sv.MSSV);
                KetQuaHoc MaxResult = new KetQuaHoc();
                if (result.Count() > 1)
                    MaxResult = LayKetQuaCaoNhat(result.ToList());
                else if (result.Count() == 1)
                    MaxResult = result.FirstOrDefault();
                else
                    continue;
                if (maxResultList.Count == 0)
                {
                    maxResultList.Add(MaxResult.MonHocID);
                    if (MaxResult.DTB != null)
                    {
                        DTB += MaxResult.DTB.Value * MaxResult.MonHoc.SoDVHocTap.Value;
                        SoTC += MaxResult.MonHoc.SoDVHocTap.Value;
                    }
                }
                else
                {
                    if (maxResultList.IndexOf(MaxResult.MonHocID) == -1)
                    {
                        maxResultList.Add(MaxResult.MonHocID);
                        if (MaxResult.DTB != null)
                        {
                            DTB += MaxResult.DTB.Value * MaxResult.MonHoc.SoDVHocTap.Value;
                            SoTC += MaxResult.MonHoc.SoDVHocTap.Value;
                        }
                    }
                    else
                        continue;
                }

            }
            if (SoTC == 0)
                return 0;
            return (DTB / SoTC);

        }

        public float TinhDTBTichLuyCacMonHocKhoaQuanLy(SinhVien sv, Khoa khoa)
        {
            var ketQuaList = (from m in entities.KetQuaHocs
                              where m.MonHoc.KhoaID.Equals(khoa.KhoaID)
                              && m.MSSV.Equals(sv.MSSV)
                              orderby m.MonHocID
                              select m
                         );
            List<MonHoc> monHocIDList = new List<MonHoc>();
            foreach (var ketQuaItem in ketQuaList)
            {
                if (monHocIDList.IndexOf(ketQuaItem.MonHoc) == -1)
                    monHocIDList.Add(monHocRepos.LayMonHocTheoMaMH(ketQuaItem.MonHocID));
            }
            float SoTinChiTL = 0, DTBChuaChia = 0;
            List<KetQuaHoc> trungMon = new List<KetQuaHoc>();
            foreach (var monHocIDItem in monHocIDList)
            {
                bool flag = false;
                foreach (var ketQuaItem in ketQuaList)
                {
                    if (ketQuaItem.MonHocID.Equals(monHocIDItem))
                    {
                        flag = true;
                        if (trungMon.IndexOf(ketQuaItem) == -1)
                            trungMon.Add(ketQuaItem);
                    }
                    if (flag && !ketQuaItem.MonHocID.Equals(monHocIDItem))
                        break;
                }
                foreach (var trungMonitem in trungMon)
                {
                    SoTinChiTL += monHocIDItem.SoDVHocTap.Value;
                    DTBChuaChia += trungMonitem.DTB.Value * monHocIDItem.SoDVHocTap.Value;
                }
            }
            return DTBChuaChia / SoTinChiTL;

        }

        public int TinhSoTCTichLuy(SinhVien sv, string NganhID, string HeDaoTaoID)
        {
            var monHocIDTrongCTDT = ctdtRepos.LayMonHocIDChuongTrinhDaoTao(NganhID, sv.Lop.NienKhoaID.Value, HeDaoTaoID).ToList();

            float SoTC = 0;

            //cac mon hoc trong ctdt cua nganh, hedaotao cua sv

            var monHocCTDT = from m in entities.KetQuaHocs
                             where m.MSSV.Equals(sv.MSSV)
                             && monHocIDTrongCTDT.Contains(m.MonHocID)
                             select m;

            List<string> maxResultList = new List<string>();
            foreach (var monHocItem in monHocCTDT)
            {
                var result = LayKetQuaHocTapTheoSVMonHoc(monHocItem.MonHocID, sv.MSSV);
                KetQuaHoc MaxResult = new KetQuaHoc();
                if (result.Count() > 1)
                    MaxResult = LayKetQuaCaoNhat(result.ToList());
                else if (result.Count() == 1)
                    MaxResult = result.FirstOrDefault();
                else
                    continue;
                if (maxResultList.Count == 0)
                {
                    maxResultList.Add(MaxResult.MonHocID);
                    if (MaxResult.DTB != null && MaxResult.DTB.Value >= 5)
                    {

                        SoTC += MaxResult.MonHoc.SoDVHocTap.Value;
                    }
                }
                else
                {
                    if (maxResultList.IndexOf(MaxResult.MonHocID) == -1)
                    {
                        maxResultList.Add(MaxResult.MonHocID);
                        if (MaxResult.DTB != null && MaxResult.DTB.Value >= 5)
                        {

                            SoTC += MaxResult.MonHoc.SoDVHocTap.Value;
                        }
                    }
                    else
                        continue;
                }

            }
            return int.Parse(SoTC.ToString());
        }

        /// <summary>
        /// lay tat ca cac ket qua hoc tap co dtb >=5 cua sv lay diem cao nhat neu trung mon hoc
        /// </summary>
        /// <param name="sv"></param>
        /// <returns></returns>
        public List<KetQuaHoc> LayTatCaKetQuaMonKhongTrungCuaSinhVien(SinhVien sv)
        {
            var kq = (from m in entities.KetQuaHocs
                      where m.MSSV.Equals(sv.MSSV)
                      && m.DTB.Value >= 5
                      select m).ToList();
            List<KetQuaHoc> result = new List<KetQuaHoc>();
            for (int i = 0; i < kq.Count(); i++)
            {
                List<KetQuaHoc> trungList = new List<KetQuaHoc>();
                for (int j = i + 1; j < kq.Count(); j++)
                {
                    if (kq[i].MonHocID.Equals(kq[j].MonHocID))
                    {
                        if (trungList.IndexOf(kq[i]) == -1)
                            trungList.Add(kq[i]);
                        if (trungList.IndexOf(kq[j]) == -1)
                            trungList.Add(kq[j]);
                    }
                }
                if (trungList.Count > 0)
                {
                    KetQuaHoc resultItem = LayKetQuaCaoNhat(trungList);
                    if (result.IndexOf(resultItem) == -1)
                        result.Add(resultItem);
                }
            }
            return result;
        }

        public List<KetQuaHoc> LayTatCaKetQuaMonDaHocKhongTrungCuaSinhVien(SinhVien sv)
        {
            var kq = (from m in entities.KetQuaHocs
                      where m.MSSV.Equals(sv.MSSV)
                      
                      select m).ToList();
            List<KetQuaHoc> result = new List<KetQuaHoc>();
            for (int i = 0; i < kq.Count(); i++)
            {
                List<KetQuaHoc> trungList = new List<KetQuaHoc>();
                for (int j = i + 1; j < kq.Count(); j++)
                {
                    if (kq[i].MonHocID.Equals(kq[j].MonHocID))
                    {
                        if (trungList.IndexOf(kq[i]) == -1)
                            trungList.Add(kq[i]);
                        if (trungList.IndexOf(kq[j]) == -1)
                            trungList.Add(kq[j]);
                    }
                }
                if (trungList.Count > 0)
                {
                    KetQuaHoc resultItem = LayKetQuaCaoNhat(trungList);
                    if (result.IndexOf(resultItem) == -1)
                        result.Add(resultItem);
                }
            }
            return result;
        }

        public List<KetQuaHoc> LayTatCaKetQuaMonDaHocKhongTrungCuaSinhVienTruMon(SinhVien sv, string MonHocID)
        {
            var kq = (from m in entities.KetQuaHocs
                      where m.MSSV.Equals(sv.MSSV)
                      && !m.MonHocID.Equals(MonHocID)
                      select m).ToList();
            List<KetQuaHoc> result = new List<KetQuaHoc>();
            for (int i = 0; i < kq.Count(); i++)
            {
                List<KetQuaHoc> trungList = new List<KetQuaHoc>();
                for (int j = i + 1; j < kq.Count(); j++)
                {
                    if (kq[i].MonHocID.Equals(kq[j].MonHocID))
                    {
                        if (trungList.IndexOf(kq[i]) == -1)
                            trungList.Add(kq[i]);
                        if (trungList.IndexOf(kq[j]) == -1)
                            trungList.Add(kq[j]);
                    }
                }
                if (trungList.Count > 0)
                {
                    KetQuaHoc resultItem = LayKetQuaCaoNhat(trungList);
                    if (result.IndexOf(resultItem) == -1)
                        result.Add(resultItem);
                }
            }
            return result;
        }

        /// <summary>
        /// 0: Tất cả
        /// 1: Bắt buộc
        /// </summary>
        /// <param name="maSV"></param>
        /// <param name="BatBuocTuChon"></param>
        /// <returns></returns>
        /// 
        public void DSMonHocTichLuy(string maSV, int BatBuoc_TuChon, out List<MonHoc> dsMHDaHT, out List<MonHoc> dsMHChuaHT, out List<MonHoc> dsGDQPDaHT, out List<MonHoc> dsGDQPChuaHT)
        {
            dsMHDaHT = new List<MonHoc>();
            dsMHChuaHT = new List<MonHoc>();
            dsGDQPDaHT = new List<MonHoc>();
            dsGDQPChuaHT = new List<MonHoc>();

            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            IChuongTrinhDaoTaoRepository ctDaoTaoRepo = new ChuongTrinhDaoTaoRepository();

            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            HeDaoTao heDaoTao = new HeDaoTao();
            khoiLopRepo.KhoiLop(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);
            string maMHDGRL = new MonHocRepository().MonHocDGRL().MonHocID;

            IQueryable<ChuongTrinhDaoTao> chTrDaoTao = ctDaoTaoRepo.LayChuongTrinhDaoTao(nganhDaoTao.NganhID, nienKhoa.NienKhoaID, heDaoTao.HeDaoTaoID);

            if (BatBuoc_TuChon == 0)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from m in entities.KetQuaHocs
                                where m.MSSV == maSV
                                && m.MonHocID == mh.MonHocID
                                && m.DTB.Value >= 5
                                && m.MonHocID != maMHDGRL
                                select m).Distinct().FirstOrDefault();
                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }

                    else
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }

            }
            else if (BatBuoc_TuChon == 1)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from k in entities.KetQuaHocs
                                join ctdt in entities.ChuongTrinhDaoTaos
                                on k.MonHocID equals ctdt.MonHocID
                                where k.MSSV == maSV
                                && ctdt.TuChon == false
                                && k.DTB >= 5
                                && k.MonHocID != maMHDGRL
                                && k.MonHocID == mh.MonHocID
                                select k).Distinct().FirstOrDefault();

                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }

                    if (daHT == null && mh.TuChon == false)
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }

            }
            else if (BatBuoc_TuChon == 2)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from k in entities.KetQuaHocs
                                join ctdt in entities.ChuongTrinhDaoTaos
                                on k.MonHocID equals ctdt.MonHocID
                                where k.MSSV == maSV
                                && ctdt.TuChon == true
                                && k.DTB >= 5
                                && k.MonHocID != maMHDGRL
                                && ctdt.NganhID == nganhDaoTao.NganhID
                                && ctdt.HeDaoTaoID == heDaoTao.HeDaoTaoID
                                && k.MonHocID == mh.MonHocID
                                select k).FirstOrDefault();
                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                    if (daHT == null && mh.TuChon == true)
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sv"></param>
        /// <param name="nienKhoaID"></param>
        /// <param name="nganhID"></param>
        /// <param name="heDaoTaoID"></param>
        /// <param name="BatBuoc_TuChon">1 - bat buoc; 0- tat ca</param>
        /// <param name="dsMHDaHT"></param>
        /// <param name="dsMHChuaHT"></param>
        public void DSMonHocTichLuy(SinhVien sv, int nienKhoaID, string nganhID, string heDaoTaoID, int BatBuoc_TuChon, out List<MonHoc> dsMHDaHT, out List<MonHoc> dsMHChuaHT)
        {
            dsMHDaHT = new List<MonHoc>();
            dsMHChuaHT = new List<MonHoc>();

            // KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            IChuongTrinhDaoTaoRepository ctDaoTaoRepo = new ChuongTrinhDaoTaoRepository();

            //NganhDaoTao nganhDaoTao = new NganhDaoTao();
            //NienKhoa nienKhoa = new NienKhoa();
            //HeDaoTao heDaoTao = new HeDaoTao();
            //khoiLopRepo.KhoiLop(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);

            IQueryable<ChuongTrinhDaoTao> chTrDaoTao = ctDaoTaoRepo.LayChuongTrinhDaoTao(nganhID, nienKhoaID, heDaoTaoID);

            if (BatBuoc_TuChon == 0)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from m in entities.KetQuaHocs
                                where m.MSSV == sv.MSSV
                                && m.MonHocID == mh.MonHocID
                                && m.DTB.Value >= 5
                                select m).Distinct().FirstOrDefault();
                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                    else
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }

            }
            else if (BatBuoc_TuChon == 1)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from k in entities.KetQuaHocs
                                join ctdt in entities.ChuongTrinhDaoTaos
                                on k.MonHocID equals ctdt.MonHocID
                                where k.MSSV == sv.MSSV
                                && ctdt.TuChon == false
                                && k.DTB >= 5
                                && k.MonHocID == mh.MonHocID
                                select k).Distinct().FirstOrDefault();

                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }

                    if (daHT == null && mh.TuChon == false)
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }

            }
            else if (BatBuoc_TuChon == 2)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from k in entities.KetQuaHocs
                                join ctdt in entities.ChuongTrinhDaoTaos
                                on k.MonHocID equals ctdt.MonHocID
                                where k.MSSV == sv.MSSV
                                && ctdt.TuChon == true
                                && k.DTB >= 5
                                && ctdt.NganhID == nganhID
                                && ctdt.HeDaoTaoID == heDaoTaoID
                                && k.MonHocID == mh.MonHocID
                                select k).FirstOrDefault();
                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                    if (daHT == null && mh.TuChon == true)
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }
            }
        }

        public List<KetQuaHoc> DSMonHocTuChonTichLuy(string maSV)
        {
            throw new NotImplementedException();
        }

        public KetQuaHoc LayMonAVGTCaoNhatDaHT(string maSV)
        {
            MonHocRepository monHocRepo = new MonHocRepository();
            var dsmhAV = monHocRepo.NhomAVGT();
            var dsmhAVdaHT = new List<KetQuaHoc>();
            foreach (var monAV in dsmhAV)
            {
                var avHT = (from m in entities.KetQuaHocs
                            where m.MSSV == maSV
                            && m.MonHocID == monAV.MonHocID
                            && m.DTB >= 5
                            select m).FirstOrDefault();
                if (avHT != null)
                    dsmhAVdaHT.Add(avHT);
            }

            var dsmhDiemGiamDan = (from m in dsmhAVdaHT
                                   orderby m.MonHocID descending
                                   select m);

            return dsmhDiemGiamDan.FirstOrDefault();
        }

        public bool KiemTraSinhVienHocLucYeu(SinhVien sv, string NganhID)
        {
            var danhGia = (from m in entities.ThangDiemDanhGias
                           where m.XepLoai.ToLower().Equals("yếu")
                           select m).FirstOrDefault();
            var sv_tichLuy = (from m in entities.SinhVien_TichLuy
                              where m.MSSV.Equals(sv.MSSV)
                              && m.NganhID.Equals(NganhID)
                              && m.HeDaoTaoID.Equals(sv.Lop.HeDaoTaoID)
                              select m).FirstOrDefault();
            if (sv_tichLuy.TrungBinhTichLuy.Value >= danhGia.TuGiaTri.Value && sv_tichLuy.TrungBinhTichLuy.Value <= danhGia.DenGiaTri.Value)
                return true;
            return false;
        }

        HocKyRepository hocKyRepository = new HocKyRepository();
        /// <summary>
        /// kiem tra mon hoc cua sv tai hocKyID co kq cao nhat ko
        /// </summary>
        /// <param name="MonHocID"></param>
        /// <param name="sv"></param>
        /// <param name="NganhID"></param>
        /// <param name="HeDaoTaoID"></param>
        /// <param name="HocKyID"></param>
        /// <returns></returns>
        public bool KiemTraMonHocCuaSVTaiHocKy(string MonHocID, SinhVien sv, string NganhID, string HeDaoTaoID, int HocKyID, out KetQuaHoc kqMax)
        {
            kqMax = null;
            HocKy hky = hocKyRepository.LayHocKyTheoID(HocKyID).FirstOrDefault();
            KetQuaHoc kq = LayKetQuaHocTapCaoNhatTheoSVMonHoc(MonHocID, sv.MSSV);
            if (kq != null)
            {
                if (kq.HocKy.HocKy1 == null)
                {
                    if (kq.HocKy.NamHoc.Value == hky.NamHoc && hky.HocKy1.Value == 2)
                    {
                        kqMax = kq;
                        return true;
                    }
                }
                else
                {
                    if (kq.HocKy.HocKy1.Value == 3)
                    {
                        if (kq.HocKy.NamHoc.Value == hky.NamHoc.Value && hky.HocKy1.Value == 2)
                        {
                            kqMax = kq;
                            return true;
                        }
                    }
                }
            }
            return false;
        }


        public void DSMonHocTichLuyCN2(string maSV, int BatBuoc_TuChon, out List<MonHoc> dsMHDaHT, out List<MonHoc> dsMHChuaHT)
        {
            dsMHDaHT = new List<MonHoc>();
            dsMHChuaHT = new List<MonHoc>();

            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            IChuongTrinhDaoTaoRepository ctDaoTaoRepo = new ChuongTrinhDaoTaoRepository();

            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            HeDaoTao heDaoTao = new HeDaoTao();
            khoiLopRepo.KhoiLopCN2(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);

            IQueryable<ChuongTrinhDaoTao> chTrDaoTao = ctDaoTaoRepo.LayChuongTrinhDaoTao(nganhDaoTao.NganhID, nienKhoa.NienKhoaID, heDaoTao.HeDaoTaoID);

            if (BatBuoc_TuChon == 0)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from m in entities.KetQuaHocs
                                where m.MSSV == maSV
                                && m.MonHocID == mh.MonHocID
                                && m.DTB.Value >= 5
                                select m).Distinct().FirstOrDefault();
                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                    else
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }

            }
            else if (BatBuoc_TuChon == 1)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from k in entities.KetQuaHocs
                                join ctdt in entities.ChuongTrinhDaoTaos
                                on k.MonHocID equals ctdt.MonHocID
                                where k.MSSV == maSV
                                && ctdt.TuChon == false
                                && k.DTB >= 5
                                && k.MonHocID == mh.MonHocID
                                select k).Distinct().FirstOrDefault();

                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }

                    if (daHT == null && mh.TuChon == false)
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }

            }
            else if (BatBuoc_TuChon == 2)
            {
                foreach (var mh in chTrDaoTao)
                {
                    var daHT = (from k in entities.KetQuaHocs
                                join ctdt in entities.ChuongTrinhDaoTaos
                                on k.MonHocID equals ctdt.MonHocID
                                where k.MSSV == maSV
                                && ctdt.TuChon == true
                                && k.DTB >= 5
                                && ctdt.NganhID == nganhDaoTao.NganhID
                                && ctdt.HeDaoTaoID == heDaoTao.HeDaoTaoID
                                && k.MonHocID == mh.MonHocID
                                select k).FirstOrDefault();
                    if (daHT != null)
                    {
                        dsMHDaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                    if (daHT == null && mh.TuChon == true)
                    {
                        dsMHChuaHT.Add(monHocRepos.LayMonHocTheoMaMH(mh.MonHocID));
                    }
                }
            }
        }

        public void DSMHGDQPTichLuy(string maSV, out List<MonHoc> dsMHDaHT, out List<MonHoc> dsMHChuaHT)
        {
            dsMHDaHT = new List<MonHoc>();
            dsMHChuaHT = new List<MonHoc>();



            throw new NotImplementedException();
        }


        public void DSMonHocTichLuy(string maSV, int BatBuoc_TuChon, out List<MonHoc> dsMHDaHT, out List<MonHoc> dsMHChuaHT)
        {
            throw new NotImplementedException();
        }



        public List<KetQuaHoc> LayTatCaDiemRenLuyen(string maSV)
        {
            string maMHDGRL = new MonHocRepository().MonHocDGRL().MonHocID;
            var rlList = (from m in entities.KetQuaHocs
                          where m.MSSV == maSV
                          && m.MonHocID == maMHDGRL
                          orderby m.HocKy.NamHoc, m.HocKy.HocKy1 // xep theo thu tu tang dan theo Nam, theo HocKy
                          select m).ToList();
            if(rlList.Count!=0)
                if (rlList.Last().HocKy.HocKy1 == 1)
                    rlList.Remove(rlList.Last());
            return rlList;
        }


        public List<int?> LayDSNamHocTuDSDiemRL(List<KetQuaHoc> rlList)
        {
            var kq = (from m in rlList
                      select m.HocKy.NamHoc).Distinct().ToList();
            return kq;
        }


        public int DiemRLCua(int hocKy, int namHoc, List<KetQuaHoc> rlList)
        {
            var abc = (from m in rlList
                       where m.HocKy.HocKy1 == hocKy
                       && m.HocKy.NamHoc == namHoc

                       select m.DTB).FirstOrDefault();
            var kq = int.Parse(abc.ToString());
            return kq;
        }

        public int DiemRLTBNam(int diemHK1, int diemHK2)
        {
            return (diemHK1 + diemHK2) / 2;
        }

        public List<int> TinhDiemRenLuyenNam(List<KetQuaHoc> rlList)
        {
            var dsDiemNam = new List<int>();
            var dsNam = LayDSNamHocTuDSDiemRL(rlList);
            int diemTBNam = 0;

            foreach (var nam in dsNam)
            {
                diemTBNam = DiemRLTBNam(DiemRLCua(1, nam.Value, rlList), DiemRLCua(2, nam.Value, rlList));
                dsDiemNam.Add(diemTBNam);
            }

            return dsDiemNam;
        }

        public int DiemTBRLToanKhoa(List<int> diemRLNamList)
        {
            int heso = 0;
            int index = 0;
            int tongDiemRL = 0;
            foreach (var diemRLNam in diemRLNamList)
            {
                index = diemRLNamList.IndexOf(diemRLNam);
                if (index + 1 == 4)
                {
                    tongDiemRL += diemRLNam;
                    heso += 2;
                    return (tongDiemRL / heso);
                }
                tongDiemRL += diemRLNam;
                heso += 1;
            }
            if (heso == 0)
                return 0;
            return tongDiemRL / heso;
        }


        #region IKetQuaHocRepository Members

        /// <summary>
        /// tinh dtbtl cua dsmh
        /// </summary>
        /// <param name="monHocList"></param>
        /// <returns></returns>
        public float TinhDTBTichLuyTheoDsMH(List<KetQuaHoc> monHocList)
        {
            float dtb = 0;
            float tinchi = 0;
            foreach (var kqItem in monHocList)
            {
                
                if (kqItem != null)
                {
                    tinchi += kqItem.MonHoc.SoDVHocTap.Value;
                    dtb += kqItem.DTB.Value * kqItem.MonHoc.SoDVHocTap.Value;
                }
            }
            return dtb / tinchi;
        }

        #endregion

        #region IKetQuaHocRepository Members

        /// <summary>
        /// kiem tra dssv da tug hoc va co diem tat ca mh trog dsMonHoc va tinh dtb
        /// </summary>
        /// <param name="dsMonHoc"></param>
        /// <param name="dtbtlKhoi"></param>
        /// <returns></returns>
        public bool LayDanhSachSVDaHocDsMH(SinhVien sv, List<MonHoc> dsMonHoc, MonHoc monHocKhongTinh, out float dtbtlKhoi,out float tinchiKhoi)
        {
            float dtb = 0;
            float tinchi = 0;
            dtbtlKhoi=0;
            tinchiKhoi = 0;
            List<string> monHocTemp = new List<string>();
            foreach (var monHocItem in dsMonHoc)
            {
                if (monHocItem.MonHocID.Equals(monHocKhongTinh.MonHocID))
                {
                    var ketQuatemp = from m in entities.KetQuaHocs
                                 where m.MonHocID.Equals(monHocItem.MonHocID)
                                 && m.MSSV.Equals(sv.MSSV)
                                 select m;
                    if (ketQuatemp.Count() > 0)
                        continue;
                    else
                        return false;
                }
                if (monHocItem.MonHocID.Equals("502002") || monHocItem.MonHocID.Equals("502018") || monHocItem.MonHocID.Equals("402021"))
                {
                    if (monHocTemp.IndexOf("502002") == -1 && monHocTemp.IndexOf("502018") == -1 && monHocTemp.IndexOf("402021") == -1)
                    {
                        //if (monHocItem.MonHocID.Equals("502002") || monHocItem.MonHocID.Equals("502018") || monHocItem.MonHocID.Equals("402021"))
                        //{
                            var ketQuatemp = from m in entities.KetQuaHocs
                                             where (m.MonHocID.Equals("502002")
                                         || m.MonHocID.Equals("502018")
                                         || m.MonHocID.Equals("402021"))
                                         && m.MSSV.Equals(sv.MSSV)
                                             orderby m.DTB descending
                                             select m;
                            monHocTemp.Add("502002");
                            monHocTemp.Add("502018");
                            monHocTemp.Add("402021");
                            if (ketQuatemp.Count() > 0)
                            {
                                
                                dtb += ketQuatemp.FirstOrDefault().DTB.Value * ketQuatemp.FirstOrDefault().MonHoc.SoDVHocTap.Value;
                                tinchi += ketQuatemp.FirstOrDefault().MonHoc.SoDVHocTap.Value;
                            }
                       // }
                        continue;
                    }
                }
                
                var ketQua = from m in entities.KetQuaHocs
                             where m.MonHocID.Equals(monHocItem.MonHocID)
                             && m.MSSV.Equals(sv.MSSV)
                             orderby m.DTB descending
                             select m;
                if (ketQua.Count() == 0)
                    return false;
                //if (ketQua.Count() > 1)
                //{
                //    KetQuaHoc max = LayKetQuaCaoNhat(ketQua.ToList());
                //    dtb += max.DTB.Value * max.MonHoc.SoDVHocTap.Value;
                //    tinchi += max.MonHoc.SoDVHocTap.Value;
                //}
                //else if (ketQua.Count() == 1)
                //{

                if (ketQua.Count() > 0)
                {
                    dtb += ketQua.FirstOrDefault().DTB.Value * ketQua.FirstOrDefault().MonHoc.SoDVHocTap.Value;
                    tinchi += ketQua.FirstOrDefault().MonHoc.SoDVHocTap.Value;
                }
                //}
            }
            dtbtlKhoi = dtb / tinchi;
            tinchiKhoi = tinchi;
            return true;
        }

        #endregion

        #region IKetQuaHocRepository Members


        public List<MonHoc> LayDanhSachMonHocDaHocCuaSVTheoKhoi(SinhVien sv, List<MonHoc> dsMonHoc)
        {
            List<MonHoc> result = new List<MonHoc>();
            foreach (var monHocItem in dsMonHoc)
            {
                var ketQua = from m in entities.KetQuaHocs
                            where m.MSSV.Equals(sv.MSSV)
                            && m.MonHocID.Equals(monHocItem.MonHocID)
                            select m;
                if (ketQua.Count() > 0)
                    result.Add(monHocItem);
            }
            return result;
        }

        #endregion

        #region IKetQuaHocRepository Members


        public List<KetQuaHoc> LayDSSVTheoKhoaVaMonHoc(string HeDaoTaoID, string NganhID, int NienKhoaID, string MonHocID)
        {
            ISinhVienRepository svRepos = new SinhVienRepository();
            List<SinhVien> dssv = svRepos.LayDsSVTheoKhoaHoc(NganhID,HeDaoTaoID,NienKhoaID);
            if (NienKhoaID != 2008)
            {
                foreach (var sinhVienItem in svRepos.LayDsSVTheoKhoaHoc(NganhID, HeDaoTaoID, NienKhoaID - 1))
                {
                    dssv.Add(sinhVienItem);
                }
            }
            List<KetQuaHoc> result = new List<KetQuaHoc>();
            foreach(var sinhVienItem in dssv)
            {
                var ketqua = from m in entities.KetQuaHocs
                             where m.MSSV.Equals(sinhVienItem.MSSV)
                             && m.MonHocID.Equals(MonHocID)
                             select m;
                if (ketqua != null && ketqua.Count()!=0)
                    result.Add(LayKetQuaCaoNhat(ketqua.ToList()));
            }
            return result;
        }

        #endregion

        public List<KetQuaHoc> ds4mon(string masv)
        {
            var list = (from m in entities.KetQuaHocs
                        where m.MSSV == masv
                        && (m.MonHocID == "503003" || m.MonHocID == "503004" || m.MonHocID == "504009" || m.MonHocID == "504017")
                        orderby m.DTB descending
                        select m).ToList();
            return list;
        }

        public List<string> dssv1d2d()
        {
            var kq = (from m in entities.SinhViens
                      where (m.Lop.TenLop == "08TH1D") || (m.Lop.TenLop == "08TH2D")
                      select m.MSSV).ToList();
            return kq;            
        }


        public float? diemtrb4mon(List<KetQuaHoc> ds4mon)
        {            
            float? tongdiem = 0;
            float? tongTC = 0;
            KetQuaHoc tmon = null ;
            foreach (var mon in ds4mon)
            {
                if (tmon == null)
                    continue;
                if (tmon.MonHocID == mon.MonHocID)
                    continue;
                
                tongdiem += mon.DTB;
                tongTC += mon.MonHoc.SoDVHocTap;

                tmon = mon;
            }
            return tongdiem / tongTC;
        }






        public IQueryable<KetQuaHoc> LayTatCaDSSVDauMH(string monHocID, string nganhID, string heDaoTaoID)
        {
            return from m in entities.KetQuaHocs
                   where m.MonHocID.Equals(monHocID)
                   
                   orderby m.MSSV
                   select m;
        }

        public IQueryable<KetQuaHoc> LayTatCaDSSVRotMH(string monHocID, string nganhID, string heDaoTaoID)
        {
            return from m in entities.KetQuaHocs
                   where m.MonHocID.Equals(monHocID)
                   && m.DTB.Value < 5
                   orderby m.MSSV
                   select m;
        }
    }
}
